package fu.capstone.hanoicityguide.dataaccess.taxi;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import fu.capstone.hanoicityguide.business.model.Taxi;
import fu.capstone.hanoicityguide.dataaccess.base.BaseDAOImpl;
import fu.capstone.hanoicityguide.dataaccess.base.DatabaseHelper;

public class TaxiDAOImp extends BaseDAOImpl implements TaxiDAO {

	private SQLiteDatabase database;
	private DatabaseHelper dbHelper;
	private Context context;

	public TaxiDAOImp(Context ctx) {
		super(ctx);
		// TODO Auto-generated constructor stub
	}

	@Override
	public ArrayList<Taxi> listTaxi(String filter, String sorter) {
		// TODO Auto-generated method stub
		Cursor cursor = null;
		ArrayList<Taxi> arrResult = new ArrayList<Taxi>();
		
		try {
			dbHelper = connectDB();
			database = dbHelper.getReadableDatabase();
			String queryTaxi = "SELECT * FROM Transport " +
					"LEFT JOIN TransportCategory ON Transport.[TransportID] = TransportCategory.[Id]  " +
					"WHERE TransportCategory.[TransportName] == \"Taxi\"";

			cursor = database.rawQuery(queryTaxi, null);
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {

						Taxi taxi = new Taxi();
						taxi.setId(cursor.getInt(cursor
								.getColumnIndex("Id")));
						taxi.setName(cursor.getString(cursor
								.getColumnIndex("Name")));
						taxi.setDesc(cursor.getString(cursor
								.getColumnIndex("PhoneNumber")));
						taxi.setIconPath(cursor.getString(cursor
								.getColumnIndex("IconPath")));

						arrResult.add(taxi);
					} while (cursor.moveToNext());
				}
			}
		} catch (Exception e) {
			Log.v("Can't query DB", e.toString());
		} finally {
			closeDB(database, dbHelper);
		}
		
		return arrResult;
	}

}
